package 单链表;

import java.util.List;

public class removeNthFromEnd {

    //删除链表中倒数第n个节点
    public ListNode removeNthFromEnd(ListNode head, int n) {
           ListNode dummy=new ListNode(-1);
           dummy.next=head;
           ListNode fast=dummy;
           //先移动n
           while (n-->0){
               fast=fast.next;
           }
           //k+1-n处
           ListNode slow=dummy;
           //fast在移动 k-n个
           while (fast.next!=null){
               fast=fast.next;
               slow=slow.next;
           }
           //k+1 -k+n =倒数n+1的位置上面
           slow.next=slow.next.next;
           return dummy.next;
    }
}
